import { defineStore } from "pinia";
import { ref } from "vue";
import {
  delayTaskAPI, deliverTaskAPI, fetchTaskListAPI, reportExceptionAPI, takeDeliveryAPI,
  truckRegistrationAPI,
} from "@/api/taskApi";

const useTaskStore = defineStore("taskStore", () => {
  /*待提货*/
  const pickupList = ref([]);
  const pickupCounts = ref(0);
  const fetchPickupList = async (params) => {
    const { data } = await fetchTaskListAPI(params);
    pickupCounts.value = data.counts;
    pickupList.value.push(...data.items);
    params.page++;
  };

  /*在途*/
  const deliveryList = ref([]);
  const fetchDeliveryList = async (params) => {
    const { data } = await fetchTaskListAPI(params);
    deliveryList.value = data.items;
  };

  /*已完成*/
  const completeList = ref([]);
  const completeCounts = ref(0);
  const fetchCompleteList = async (params) => {
    const { data } = await fetchTaskListAPI(params);
    completeCounts.value = data.counts;
    completeList.value.push(...data.items);
    params.page++;
  };


  /*延迟提货的请求函数*/
  const delayTask = async (formData) => {
    await delayTaskAPI(formData);
  };

  /*提货的请求函数*/
  const takeDelivery = async (formData) => {
    await takeDeliveryAPI(formData);
  };

  /*上报异常的请求函数*/
  const reportException = async (formData) => {
    await reportExceptionAPI(formData);
  };

  /*交付的请求函数*/
  const deliverTask = async (formData) => {
    await deliverTaskAPI(formData);
  };

  /*回车登记的请求函数*/
  const truckRegistration = async (formData) => {
    await truckRegistrationAPI(formData);
  };
  return {
    pickupCounts,
    pickupList,
    deliveryList,
    completeCounts,
    completeList,
    fetchPickupList,
    fetchCompleteList,
    delayTask,
    takeDelivery,
    fetchDeliveryList,
    reportException,
    deliverTask,
    truckRegistration,
  };
});

export default useTaskStore;
